package com.gtlab1207.br_awareness.ctrls;

import com.gtlab1207.br_awareness.common.Codes;
import com.gtlab1207.br_awareness.common.PageUtil;
import com.gtlab1207.br_awareness.common.Result;
import com.gtlab1207.br_awareness.models.domain.User;
import com.gtlab1207.br_awareness.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户信息业务控制层
 */
@RestController
@RequestMapping("/api/user")
public class UserCtrl extends BaseCtrl {
    @Autowired
    private UserService userService;

    /**
     * 查询用户，可分页
     * @param phone
     * @param nickname
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/info")
    public Result selectUser(@RequestParam(value = "phone",required = false)String phone,
                             @RequestParam(value = "nickname",required = false)String nickname,
                             @RequestParam(value = "email",required = false)String email,
                             @RequestParam(value = "pageNum",defaultValue ="1")Integer pageNum,
                             @RequestParam(value = "pageSize",defaultValue ="5")Integer pageSize){
        try {
            Map<String, Object> params = PageUtil.setPageInfo(pageNum, pageSize);
            if(email!=null){
                phone=phone.trim();
            }
            if(email!=null){
                nickname=nickname.trim();
            }
            if(email!=null){
                email=email.trim();
            }
            params.put("phone",phone);
            params.put("nickname",nickname);
            params.put("email",email);
            Map<String, Object> map = userService.selectUser(params, pageSize);
            return this.send(map);
        } catch (Exception e) {
            e.printStackTrace();
            return this.send(Codes.FAILED);
        }
    }

    /**
     * 注册用户信息
     * @param user
     * @return
     */
    @PostMapping("/info")
    public Result addUser(@Valid User user){
        try {
            if(user.getPhone()!=null&&!user.getPhone().equals("")){
                Map<String, Object> params = userService.getUserByPhone(user.getPhone());
                if(params.get("data")!=null){
                    return this.send(Codes.NUM_EXIST);
                }
            }
            Integer result = userService.insertUser(user);
            if(result==0){
                return this.send(Codes.INSERT_FAILED);
            }
            return this.send(Codes.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return this.send(Codes.FAILED);
        }
    }

    /**
     * 通过id获取用户信息
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public Result selectUserById(@PathVariable Integer id){
        try {
            Map<String, Object> map = userService.getUserById(id);
            if(map.get("data")==null){
                return this.send(Codes.NO_EXIST);
            }
            return this.send(map);
        } catch (Exception e) {
            e.printStackTrace();
            return this.send(Codes.FAILED);
        }
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @PutMapping("/info")
    public Result updateUser(@Valid User user,
                             HttpServletRequest request){
        try {

            Integer result = userService.updateUser(user);
            if(result==0){
                return this.send(Codes.MOD_FAILED);
            }
            return this.send(Codes.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return this.send(Codes.FAILED);
        }
    }

    /**
     * 根据用户id来删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/info/{id}")
    public Result deleteUser(@PathVariable Integer id){
        try {
            Integer result = userService.deleteUser(id);
            if(result==0){
                return this.send(Codes.NO_EXIST);
            }
            Map map = new HashMap();
            map.put("result",result);
            return this.send(map);
        } catch (Exception e) {
            e.printStackTrace();
            return this.send(Codes.FAILED);
        }
    }

}
